package com.gosling.cloudspace.event;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.sprometheus.core.anno.SPrometheus;
import com.sprometheus.core.constants.SPrometheusTitleEnum;
import com.sprometheus.core.constants.Scene;
import com.sprometheus.core.constants.SendChannel;
import com.sprometheus.core.handler.SPrometheusUtil;
import com.sprometheus.core.pojo.Exp;
import com.sprometheus.core.pojo.SPrometheusException;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * CustomEvent 指定了类型，所以只能监听该类型的事件
 *
 * @author lihonglin
 * @date 2024/9/4 下午2:48
 */
@Component
public class CustomEventListener implements ApplicationListener<CustomEvent> {

//    @Resource
//    SPrometheusHandler sPrometheusHandler;

    @Override
    public void onApplicationEvent(CustomEvent req) {
        System.out.println("已经收到监听事件，消费者...: " + req.getMessage());
        if (1 == 1) {
            Exp exp = new Exp();
            exp.setMsg("test...已经收到监听事件");
            exp.setUri("/admin/test1");
            exp.setSendChannel(SendChannel.FEISHU);
            exp.setAtAll(1);
            exp.setReqData(JSONObject.toJSONString(req));
            exp.setTitleEnum(SPrometheusTitleEnum.OTHER);
            SPrometheusUtil.insert(exp);

            if (2 == 2) {
                throw new SPrometheusException(SPrometheusTitleEnum.OTHER, "用户信息为空");
            }
        }
    }

    @SPrometheus(title = SPrometheusTitleEnum.OTHER, uri = "admin/test1", scene = Scene.BUSINESS)
    public void test(String id) {
        if (StrUtil.isBlank(id)){
            System.out.println("主键ID为空");
            throw new NullPointerException("主键ID为空");
        }
    }

    /***
     * 方式2
     * //@EventListener(CustomEvent.class) 使用注解方式
     * 注解方式和 @Override 的 onApplicationEvent方式不能同时使用，否则会重复消费
     */
//    @EventListener(CustomEvent.class)
//    public void onApplicationEvent1(CustomEvent event) {
//        System.out.println("已经收到监听事件，消费者2...: " + event.getMessage());
//    }
}
